<?php
class Tourism_goods_base_model extends CI_Model
{
    private $table   = 'tourism_goods_base';
    private $table_2 = 'tourism_goods_attr';
    private $table_3 = 'user';
    private $table_4 = 'tourism_order';
    private $table_5 = 'tourism_order_product';

    public function total($param = array())
    {
        $this->db->from($this->table_2 . ' AS tourism_goods_attr');
        $this->db->join($this->table . ' AS tourism_goods_base', 'tourism_goods_attr.goods_id = tourism_goods_base.goods_id');
        $this->db->join($this->table_3 . ' AS user', 'user.uid = tourism_goods_base.uid');
        $this->db->join('tourism_provider_category','tourism_provider_category.id=tourism_goods_base.provider_category_id','left');
        if (!empty($param['goods_search'])) {
            $this->db->where("((`tourism_goods_base`.`goods_name` LIKE '%{$param['goods_search']}%'))");
        }
        if (!empty($param['username'])) {
            $this->db->where("((`user`.`user_name` LIKE '%{$param['username']}%') OR (`user`.`alias_name` LIKE '%{$param['username']}%') OR (`tourism_goods_base`.`uid` LIKE '%{$param['username']}%'))");
        }
        if (isset($param['provider_category_id']) &&  strlen($param['provider_category_id'])>0 ) {
            $this->db->where('tourism_goods_base.provider_category_id', $param['provider_category_id']);
        }
        if (!empty($param['uid'])) {
            $this->db->where('tourism_goods_base.uid', $param['uid']);
        }
        if (!empty($param['is_on_sale'])) {
            $this->db->where('tourism_goods_base.is_on_sale', $param['is_on_sale']);
        }
        if (!empty($param['show_flag'])) {
            $this->db->where('tourism_goods_base.show_flag', $param['show_flag']);
        }
        if (!empty($param['is_check'])) {
            $this->db->where('tourism_goods_attr.is_check', $param['is_check']);
        }
        if (!empty($param['province_id'])) {
            $this->db->where('tourism_goods_base.province_id', $param['province_id']);
        }
        if (!empty($param['city_id'])) {
            $this->db->where('tourism_goods_base.city_id', $param['city_id']);
        }
        if (!empty($param['goods_attr_id'])) {
            $this->db->where('tourism_goods_attr.goods_attr_id', $param['goods_attr_id']);
        }
        if (!empty($param['district_id'])) {
            $this->db->where('tourism_goods_base.district_id', $param['district_id']);
        }
        if (!empty($param['class_c'])) {
            $this->db->where('tourism_goods_base.category_id', $param['class_c']);
        }
        if (!empty($param['start_date'])) {
            $this->db->where(array('tourism_goods_attr.updated_at >' => $param['start_date']));
        }
        if (!empty($param['end_date'])) {
            $this->db->where(array('tourism_goods_attr.updated_at <=' => $param['end_date']));
        }
        if (!empty($param['logic_delete'])) {
            $this->db->where('logic_delete',$param['logic_delete']);
        } else {
            $this->db->where('logic_delete',1);
        }
        return $this->db->count_all_results();
    }

    public function page_list($page_num, $num, $param = array(),$status=true)
    {
        $this->db->select('tourism_goods_base.*,tourism_goods_attr.*,user.user_name,user.alias_name,tourism_provider_category.category_name AS provider_category');
        $this->db->from($this->table_2 . ' AS tourism_goods_attr');
        $this->db->join($this->table . ' AS tourism_goods_base', 'tourism_goods_attr.goods_id = tourism_goods_base.goods_id');
        $this->db->join($this->table_3 . ' AS user', 'user.uid = tourism_goods_base.uid');
        $this->db->join('tourism_provider_category','tourism_provider_category.id=tourism_goods_base.provider_category_id','left');
        if (!empty($param['goods_search'])) {
            $this->db->where("((`tourism_goods_base`.`goods_name` LIKE '%{$param['goods_search']}%'))");
        }
        if (!empty($param['username'])) {
            $this->db->where("((`user`.`user_name` LIKE '%{$param['username']}%') OR (`user`.`alias_name` LIKE '%{$param['username']}%') OR (`tourism_goods_base`.`uid` LIKE '%{$param['username']}%'))");
        }
        if (isset($param['provider_category_id']) &&  strlen($param['provider_category_id'])>0 ) {
            $this->db->where('tourism_goods_base.provider_category_id', $param['provider_category_id']);
        }
        if (!empty($param['uid'])) {
            $this->db->where('tourism_goods_base.uid', $param['uid']);
        }
        if (!empty($param['is_on_sale'])) {
            $this->db->where('tourism_goods_base.is_on_sale', $param['is_on_sale']);
        }
        if (!empty($param['show_flag'])) {
            $this->db->where('tourism_goods_base.show_flag', $param['show_flag']);
        }
        if (!empty($param['is_check'])) {
            $this->db->where('tourism_goods_attr.is_check', $param['is_check']);
        }
        if (!empty($param['province_id'])) {
            $this->db->where('tourism_goods_base.province_id', $param['province_id']);
        }
        if (!empty($param['city_id'])) {
            $this->db->where('tourism_goods_base.city_id', $param['city_id']);
        }
        if (!empty($param['goods_attr_id'])) {
            $this->db->where('tourism_goods_attr.goods_attr_id', $param['goods_attr_id']);
        }
        if (!empty($param['district_id'])) {
            $this->db->where('tourism_goods_base.district_id', $param['district_id']);
        }
        if (!empty($param['class_c'])) {
            $this->db->where('tourism_goods_base.category_id', $param['class_c']);
        }
        if (!empty($param['start_date'])) {
            $this->db->where(array('tourism_goods_attr.updated_at >' => $param['start_date']));
        }
        if (!empty($param['end_date'])) {
            $this->db->where(array('tourism_goods_attr.updated_at <=' => $param['end_date']));
        }
        if (!empty($param['logic_delete'])) {
            $this->db->where('logic_delete',$param['logic_delete']);
        } else {
            $this->db->where('logic_delete',1);
        }
        $this->db->order_by('tourism_goods_attr.goods_attr_id', 'DESC');
        if ($status) {
            $this->db->limit($page_num, $num);
        }
        return $this->db->get();
    }
    
    public function insertTourismGoodsBase($postData)
    {
        $data = array(
            'category_id'    => $postData['category_id'],
            'category_name'  => $postData['category_name'],
            'uid'            => $postData['supplierUid'],
            'goods_name'     => $postData['goods_name'],
            'limit_num'      => $postData['limit_num'],
            'limit_day'      => empty($postData['limit_day']) ? 0 : (int)$postData['limit_day'],
            'goods_native'   => $postData['goods_native'],
            'brand_id'       => !empty($postData['brand_id']) ? $postData['brand_id'] : 0,
            'province_id'    => $postData['province_id'],
            'city_id'        => $postData['city_id'],
            'district_id'    => $postData['district_id'],
            'detail'         => !empty($postData['detail']) ? $postData['detail'] : '',
            'is_on_sale'     => $postData['is_on_sale'],
            'payments'       => (count($postData['payments']) == 2) ? 1 : 1,
            'goods_brief'    => $postData['goods_brief'],
            'images'         => $postData['images'],
            'show_flag'      => !empty($postData['show_flag']) ? $postData['show_flag'] : 0,
            'keyword'        => !empty($postData['keyword']) ? $postData['keyword'] : '',
            'rate_id'        => !empty($postData['rate_id']) ? $postData['rate_id'] : 1
        );
        //附加属性
        if (!empty($postData['key']) && !empty($postData['value'])) {
            $data['goods_spec'] = $this->getGoodSpec($postData['key'], $postData['value']);
        } else {
            $data['goods_spec'] = '';
        }
        
        if (!empty($postData['wap_goods_desc'])) {
            $data['wap_goods_desc'] = $postData['wap_goods_desc'];
        }
        //运费模版
        if ($postData['transport_type'] == 1) {
            $data['transport_id'] = $postData['transport_id'];
        } else {
            $data['transport_cost'] = $postData['transport_cost'];
        }
        if (!empty($postData['provider_category_id'])) {
            $data['provider_category_id'] = $postData['provider_category_id'];
        }
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    public function updateTourismGoodsBase($postData)
    {
        $data = array(
            'category_id'    => $postData['category_id'],
            'category_name'  => $postData['category_name'],
            'uid'            => $postData['supplierUid'],
            'goods_name'     => $postData['goods_name'],
            'limit_num'      => $postData['limit_num'],
            'limit_day'      => empty($postData['limit_day']) ? 0 : (int)$postData['limit_day'],
            'goods_native'   => $postData['goods_native'],
            'brand_id'       => !empty($postData['brand_id']) ? $postData['brand_id'] : 0,
            'province_id'    => $postData['province_id'],
            'city_id'        => $postData['city_id'],
            'district_id'    => $postData['district_id'],
            'detail'         => !empty($postData['detail']) ? $postData['detail'] : '',
            'is_on_sale'     => $postData['is_on_sale'],
            'payments'       => (count($postData['payments']) == 2) ? 1 : 1,
            'goods_brief'    => $postData['goods_brief'],
            'images'         => $postData['images'],
            'show_flag'      => !empty($postData['show_flag']) ? $postData['show_flag'] : 0,
            'keyword'        => !empty($postData['keyword']) ? $postData['keyword'] : '',
            'rate_id'        => !empty($postData['rate_id']) ? $postData['rate_id'] : 1
        );
        //附加属性
        if (!empty($postData['key']) && !empty($postData['value'])) {
            $data['goods_spec'] = $this->getGoodSpec($postData['key'], $postData['value']);
        } else {
            $data['goods_spec'] = '';
        }
        if( !empty($postData['wap_goods_desc'])){
            $data['wap_goods_desc'] = $postData['wap_goods_desc'];
        }
        //运费模版
        if ($postData['transport_type'] == 1) {
            $data['transport_id']   = $postData['transport_id'];
            $data['transport_cost'] = 0;
        } else {
            $data['transport_id']   = 0;
            $data['transport_cost'] = $postData['transport_cost'];
        }
        if (!empty($postData['provider_category_id'])) {
            $data['provider_category_id'] = $postData['provider_category_id'];
        }
        $this->db->where('goods_id', $postData['goods_id']);
        return $this->db->update($this->table, $data);
    }
    
    public function getGoodSpec($keys=array(), $value=array())
    {
        if (is_array($keys) && is_array($value)) {
            foreach ($keys as $k => $key) {
                if (!isset($keys[$k]) || empty($keys[$k]) || !isset($value[$k]) || empty($value[$k])) {
                    unset($keys[$k]);
                    unset($value[$k]);
                }
            }
            $combine = array_combine($keys, $value);
            return !empty($combine) ? serialize($combine) : '';
        }
        return '';
    }
    
    /**
    * 获取商品信息
    */
    public function getGoodsAttr($goods_attr_id)
    {
        $this->db->select('tourism_goods_base.*, tourism_goods_attr.*, user.user_name, user.alias_name');
        $this->db->from($this->table_2 . ' AS tourism_goods_attr');
        $this->db->join($this->table . ' AS tourism_goods_base', 'tourism_goods_base.goods_id = tourism_goods_attr.goods_id');
        $this->db->join($this->table_3 . ' as user', 'user.uid = tourism_goods_base.uid');
        $this->db->where('tourism_goods_attr.goods_attr_id', $goods_attr_id);
        return $this->db->get();
    }
    
    /**
     * 上下架
     * @param unknown $goods_id
     * @param unknown $is_on_sale
     */
    public function setIsOnSaleStatus($goods_id, $is_on_sale)
    {
        $data = array(
            'is_on_sale'=>$is_on_sale
        );
        if (is_array($goods_id)) {
            $this->db->where_in('goods_id', $goods_id);
        } else {
            $this->db->where('goods_id', $goods_id);
        }
        return $this->db->update($this->table, $data);
    }
    

    
    public function findByAttrId($goods_attr_id)
    {
        $this->db->from($this->table . ' AS tourism_goods_base');
        $this->db->join($this->table_2 . ' AS tourism_goods_attr', 'tourism_goods_attr.goods_id = tourism_goods_base.goods_id', 'inner');
        $this->db->where('tourism_goods_attr.goods_attr_id', $goods_attr_id);
        $this->db->limit(1);
        return $this->db->get();
    }
    
    /**
     * 商品销售排行榜统计
     */
    public function salesTourismRankTotal($search_param=array())
    {
        $this->db->select('COUNT(*)');
        $this->db->from($this->table.' AS tourism_goods_base');
        $this->db->join($this->table_2.' AS tourism_goods_attr', 'tourism_goods_attr.goods_id = tourism_goods_base.goods_id');
        $this->db->join($this->table_3.' AS user', 'user.uid = tourism_goods_base.uid');
        $this->db->join($this->table_5.' AS tourism_order_product', 'tourism_order_product.goods_attr_id = tourism_goods_attr.goods_attr_id', 'left');
        $this->db->join($this->table_4.' AS tourism_order', 'tourism_order.order_id = tourism_order_product.order_id', 'left');
        if (!empty($search_param['tourism_goods'])) {
            $this->db->where("(`tourism_goods_base`.`goods_name` LIKE '%{$search_param['tourism_goods']}%')");
        }
        if (!empty($search_param['user_name'])) {
            $this->db->where("(`user`.`user_name` LIKE '%{$search_param['user_name']}%') OR (`user`.`alias_name` LIKE '%{$search_param['user_name']}%')");
        }
        if (!empty($search_param['start_date'])) {
            $this->db->where(array('tourism_order.created_at >' => $search_param['start_date']));
        }
        if (!empty($search_param['end_date'])) {
            $this->db->where(array('tourism_order.created_at <=' => $search_param['end_date']));
        }
        $this->db->where_in('tourism_order.order_status', array('3', '4', '5', '6'));
        $this->db->where('tourism_order.payment_type !=', 2); //不是到付
        $this->db->group_by('tourism_order_product.goods_attr_id');
        $result = $this->db->get();
        return $result->num_rows();
    }
    
    /**
     * 商品销售排行榜统计
     */
    public function salesTourismRankPageList($page_num, $num, $search_param=array())
    {
        $this->db->select('tourism_goods_base.goods_id, tourism_goods_base.goods_name, user.user_name, user.alias_name');
        $this->db->select_sum('tourism_order_product.refund_num');
        $this->db->select_sum('tourism_order_product.pay_amount * tourism_order_product.refund_num', 'order_pay_total');
        $this->db->select_sum('tourism_order_product.pay_amount * (tourism_order_product.number - tourism_order_product.refund_num)', 'order_refund_total');
        $this->db->from($this->table.' AS tourism_goods_base');
        $this->db->join($this->table_2.' AS tourism_goods_attr', 'tourism_goods_attr.goods_id = tourism_goods_base.goods_id');
        $this->db->join($this->table_3.' AS user', 'user.uid = tourism_goods_base.uid');
        $this->db->join($this->table_5.' AS tourism_order_product', 'tourism_order_product.goods_attr_id = tourism_goods_attr.goods_attr_id', 'left');
        $this->db->join($this->table_4.' AS tourism_order', 'tourism_order.order_id = tourism_order_product.order_id', 'left');
        if (!empty($search_param['tourism_goods'])) {
            $this->db->where("(`tourism_goods_base`.`goods_name` LIKE '%{$search_param['tourism_goods']}%')");
        }
        if (!empty($search_param['user_name'])) {
            $this->db->where("(`user`.`user_name` LIKE '%{$search_param['user_name']}%') OR (`user`.`alias_name` LIKE '%{$search_param['user_name']}%')");
        }
        if (!empty($search_param['start_date'])) {
            $this->db->where(array('tourism_order.created_at >' => $search_param['start_date']));
        }
        if (!empty($search_param['end_date'])) {
            $this->db->where(array('tourism_order.created_at <=' => $search_param['end_date']));
        }
        $this->db->where_in('tourism_order.order_status', array('3', '4', '5', '6'));
        $this->db->where('tourism_order.payment_type !=', 2); //不是到付
        $this->db->group_by('tourism_order_product.goods_attr_id');
        $this->db->order_by('SUM(tourism_order_product.pay_amount * tourism_order_product.refund_num)', 'DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }
    
    /**
     * 根据供应商分类获取商品总数
     * @param unknown $id
     */
    public function findResultByProviderCategoryId($id){
         
        $this->db->select('goods_id');
        $this->db->from('tourism_goods_base');
        $this->db->where('provider_category_id',$id);
        return $this->db->get();
    }
    
     /**
      * 更新商品的供应商分类
     */
    public function updateTourismProviderCategory($param=array()) {
    	
        if (!empty($param['category_id'])) {
        	$data['provider_category_id'] = $param['category_id'];
        }
        if (!empty($param['goods_id'])) {
        	if (is_array($param['goods_id'])) {
        		$this->db->where_in('goods_id',$param['goods_id']);
        	} else {
        	    $this->db->where('goods_id',$param['goods_id']);
        	}
        }
        return $this->db->update($this->table,$data);
    }
}